package com.chen.framework.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.root.entity.SysComConfig;
import com.chen.framework.service.inter.SysComConfigService;
import com.chen.root.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachePut;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.chen.root.constant.CacheKeyCons.COMMON_CONFIG_KEY;

/**
 * <p>
 * 公共配置 前端控制器
 * </p>
 *
 * @author 
 * @since 2025-01-14 09:58:29
 */
@Validated
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/sysComConfig")
@Api(value = "SysComConfig对象", tags = "公共配置信息管理API")
public class ComConfigController {

    private final SysComConfigService sysComConfigService;

    @PostMapping("/getValue")
    @ApiOperation(value = "根据name获取配置值", notes = "根据name获取配置值")
    public R<String> getValue(String name){
        SysComConfig one = sysComConfigService.getOne(buildWrapper(name));
        return one != null ? R.ok(one.getValue()) : R.failed(null, "配置不存在");
    }

    @GetMapping("/page" )
    @ApiOperation(value = "分页查询", notes = "分页查询")
    public R<Page<SysComConfig>> getPage(Page<SysComConfig> page, SysComConfig sysComConfig) {
        return R.ok(sysComConfigService.page(page, Wrappers.query(sysComConfig)));
    }

    @GetMapping("/{sysComConfigId}" )
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    public R<SysComConfig> getById(@PathVariable("sysComConfigId" ) Long sysComConfigId) {
        return R.ok(sysComConfigService.getById(sysComConfigId));
    }

    @PostMapping
    @ApiOperation(value = "新增公共配置", notes = "新增公共配置")
    public R<Boolean> save(@RequestBody SysComConfig sysComConfig) {
        LambdaQueryWrapper<SysComConfig> wrapper = buildWrapper(sysComConfig.getName());
        long count = sysComConfigService.count(wrapper);
        Assert.isTrue(count == 0, "配置已存在");
        return R.ok(sysComConfigService.save(sysComConfig));
    }

    @PutMapping
    @ApiOperation(value = "修改公共配置", notes = "修改公共配置")
    @CachePut(cacheNames = COMMON_CONFIG_KEY, key = "#sysComConfig.name")
    public R<Boolean> updateById(@RequestBody SysComConfig sysComConfig) {
        LambdaQueryWrapper<SysComConfig> wrapper = buildWrapper(sysComConfig.getName());
        long count = sysComConfigService.count(wrapper);
        Assert.isTrue(count == 0, "配置已存在");
        return R.ok(sysComConfigService.updateById(sysComConfig));
    }

    @DeleteMapping("/{sysComConfigId}" )
    @ApiOperation(value = "通过id删除公共配置", notes = "通过id删除公共配置")
    public R<Boolean> removeById(@PathVariable("sysComConfigId" ) Long sysComConfigId) {
        return R.ok(sysComConfigService.removeById(sysComConfigId));
    }

    private LambdaQueryWrapper<SysComConfig> buildWrapper(String name) {
        return Wrappers.lambdaQuery(SysComConfig.class)
                .select(SysComConfig::getValue).eq(SysComConfig::getName, name);
    }
}
